package com.boarsoft.boar.app.biz;

import org.springframework.stereotype.Component;

import com.boarsoft.boar.app.AppInstLibBiz;
import com.boarsoft.boar.entity.AppInstLib;
import com.boarsoft.common.Util;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("appInstLibBiz")
public class AppInstLibBizImpl extends SimpleBizImpl implements AppInstLibBiz {

	@Override
	public AppInstLib get(String id) {
		return dao.get(AppInstLib.class, id);
	}

	@Override
	public AppInstLib findByIds(String id, String appId, String libId, String fileId, String serverId) {

		boolean condition1 = Util.strIsEmpty(id);
		boolean condition2 = Util.strIsEmpty(appId) && Util.strIsEmpty(serverId);
		boolean condition3 = Util.strIsEmpty(libId) && Util.strIsEmpty(fileId);

		if (condition1 || ((condition2) || (condition3))) {
			log.error("查询条件(id={},appId={},libId={},fileId={},serverId={})有误！请检查！", id, appId, libId, fileId, serverId);
			return null;
		}

		StringBuilder sb = new StringBuilder();
		sb.append("select ail from ").append(AppInstLib.class.getName()).append(" ail where 1=1 ");
		if (Util.strIsNotEmpty(id)) {
			sb.append(" and ail.id='").append(id).append("'");
		}
		if (Util.strIsNotEmpty(appId)) {
			sb.append(" and ail.appId='").append(appId).append("'");
		}
		if (Util.strIsNotEmpty(libId)) {
			sb.append(" and ail.libId='").append(libId).append("'");
		}
		if (Util.strIsNotEmpty(fileId)) {
			sb.append(" and ail.fileId='").append(fileId).append("'");
		}
		if (Util.strIsNotEmpty(serverId)) {
			sb.append(" and ail.serverId='").append(serverId).append("'");
		}

		return dao.find(AppInstLib.class, sb.toString());
	}

	@Override
	public void save(AppInstLib o) {
		if (Util.strIsNotEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}
}
